﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using EazyCMS.DBUtility;

namespace EazyCMS.DAL
{
   public class student_score
    {
        private string databaseprefix; //数据库表名前缀
        public student_score(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
        }
        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "student_score");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

       

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.student_score model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 添加主表数据====================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into " + databaseprefix + "student_score(");
                        strSql.Append("yuwen,shuxue,yingyu,zhengzhi,wuli,huaxue,tiyu,lishi,deyu,pingshichengji,add_time,user_id)");
                        strSql.Append(" values (");
                        strSql.Append("@yuwen,@shuxue,@yingyu,@zhengzhi,@wuli,@huaxue,@tiyu,@lishi,@deyu,@pingshichengji,@add_time,@user_id)");
                        strSql.Append(";select @@IDENTITY");
                        SqlParameter[] parameters = {
					            new SqlParameter("@yuwen", SqlDbType.NVarChar,50),
					            new SqlParameter("@shuxue", SqlDbType.NVarChar,50),
					            new SqlParameter("@yingyu", SqlDbType.NVarChar,50),
					            new SqlParameter("@zhengzhi", SqlDbType.NVarChar,100),
					            new SqlParameter("@wuli", SqlDbType.NVarChar,255),
					            new SqlParameter("@huaxue", SqlDbType.NVarChar,255),
					            new SqlParameter("@tiyu", SqlDbType.NVarChar,255),
					            new SqlParameter("@lishi", SqlDbType.NVarChar,255),
					            new SqlParameter("@deyu", SqlDbType.NVarChar,255),
					            new SqlParameter("@pingshichengji", SqlDbType.NVarChar,255),
					            new SqlParameter("@add_time", SqlDbType.DateTime),
					            new SqlParameter("@user_id", SqlDbType.Int,4)};
                        parameters[0].Value = model.Yuwen;
                        parameters[1].Value = model.Shuxue;
                        parameters[2].Value = model.Yingyu;
                        parameters[3].Value = model.Zhengzhi;
                        parameters[4].Value = model.Wuli;
                        parameters[5].Value = model.Huaxue;
                        parameters[6].Value = model.Tiyu;
                        parameters[7].Value = model.Lishi;
                        parameters[8].Value = model.Deyu;
                        parameters[9].Value = model.Pingshichengji;
                        parameters[10].Value = model.Add_time;
                        parameters[11].Value = model.User_id;
                       
                        //添加主表数据
                        object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters); //带事务
                        model.Id = Convert.ToInt32(obj);
                        #endregion

                       
                        #endregion

                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();
                        return 0;
                    }
                }
            }
            return model.Id;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.student_score model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 修改主表数据==========================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update " + databaseprefix + "student_score set ");
                        strSql.Append("yuwen=@yuwen,");
                        strSql.Append("shuxue=@shuxue,");
                        strSql.Append("yingyu=@yingyu,");
                        strSql.Append("zhengzhi=@zhengzhi,");
                        strSql.Append("wuli=@wuli,");
                        strSql.Append("huaxue=@huaxue,");
                        strSql.Append("tiyu=@tiyu,");
                        strSql.Append("lishi=@lishi,");
                        strSql.Append("deyu=@deyu,");
                        strSql.Append("pingshichengji=@pingshichengji,");
                        strSql.Append("add_time=@add_time,");
                        strSql.Append("user_id=@user_id");
                       
                        strSql.Append(" where id=@id");
                        SqlParameter[] parameters = {
					             new SqlParameter("@yuwen", SqlDbType.NVarChar,50),
					            new SqlParameter("@shuxue", SqlDbType.NVarChar,50),
					            new SqlParameter("@yingyu", SqlDbType.NVarChar,50),
					            new SqlParameter("@zhengzhi", SqlDbType.NVarChar,100),
					            new SqlParameter("@wuli", SqlDbType.NVarChar,255),
					            new SqlParameter("@huaxue", SqlDbType.NVarChar,255),
					            new SqlParameter("@tiyu", SqlDbType.NVarChar,255),
					            new SqlParameter("@lishi", SqlDbType.NVarChar,255),
					            new SqlParameter("@deyu", SqlDbType.NVarChar,255),
					            new SqlParameter("@pingshichengji", SqlDbType.NVarChar,255),
					            new SqlParameter("@add_time", SqlDbType.DateTime),
					            new SqlParameter("@user_id", SqlDbType.Int,4)};
                        parameters[0].Value = model.Yuwen;
                        parameters[1].Value = model.Shuxue;
                        parameters[2].Value = model.Yingyu;
                        parameters[3].Value = model.Zhengzhi;
                        parameters[4].Value = model.Wuli;
                        parameters[5].Value = model.Huaxue;
                        parameters[6].Value = model.Tiyu;
                        parameters[7].Value = model.Lishi;
                        parameters[8].Value = model.Deyu;
                        parameters[9].Value = model.Pingshichengji;
                        parameters[10].Value = model.Add_time;
                        parameters[11].Value = model.User_id;
                       
                        DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters);
                        #endregion

                       trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
           

            //删除主表
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + databaseprefix + "student_score ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;
             List<CommandInfo> sqllist = new List<CommandInfo>();
              CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);
           
            sqllist.Add(cmd);

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
              
                return true;
            }
            else
            {
                return false;
            }
        }

      
      
    }
}
